<?php

  /**
   * Posts
   *
   * Класс виджета, выводящего посты
   */

  class Posts extends XMLWidget
  {
    /**
     * тип поста (блог, новость, публикация)
     *
     * @var string
     */
    public $type = "";

    /**
     * фильтр для выборки
     *
     * @var string
     */
    public $filter = "";

    /**
     * instance or primary key
     *
     * @var mixed
     */
    public $post = null;

    /**
     * требуется вывести только анонс
     *
     * @var bool
     */
    public $announcement = true;

    /**
     * запустить виджет
     *
     * @return void
     * @access public
     */
    public function run()
    {
      /** @var $c XMLController */
      $c = $this->controller;
      /** @var $app WebApplication */
      $app     = $c->app;
      $posts   = Post::model()->acting()->with("tags");
      $cLength = strcasecmp($this->type, Post::RESONANCE) ? 250 : 500;

      if ($this->post)
      {
        if (!($this->post instanceof Post))
          $this->post = $posts->findByPk($this->post, $this->filter);
        $defCntr = false;
        $posts   = array($this->post);
      }
      else
      {
        if (!$this->type)
          return;

        if ($defCntr = $c->isDefault)
        {
          $posts   = $posts->announcement();
          $defCntr = !strcasecmp($this->type, Post::NEWS);
        }
        $posts = $posts->{$this->type}()->findAll($this->filter);
        if (!strcasecmp($this->type, Post::NEWS))
          array_pop($posts);

        self::openNode("posts", array("type" => $this->type));
      }

      /** @var $post Post */
      foreach ($posts as $post)
      {
        $content = $post->content;
        $type    = $post->type;
        $attrs   = array("post" => $post);
        $attrs   = array(
          "id"         => $post->id,
          "can-edit"   => intval($c->checkAccess("edit_$type"  , false, $attrs)),
          "can-remove" => intval($c->checkAccess("remove_$type", false, $attrs)),
          "date"       => $app->frmDate($post->date)
        );

        if ($this->announcement)
        {
          foreach (array("p", "div", "span") as $tag)
          {
            preg_match_all("/<$tag\s+.*?class\s*=\s*['\"](.*?\s+)?lid(\s+.*?)?['\"].*?>(.*?)<\/$tag>/ui", $content, $matches, PREG_SET_ORDER);
            if (!empty($matches))
            {
              $content = "";
              foreach ($matches as $m)
                $content .= CHtml::tag("p", array(), strip_tags(htmlspecialchars_decode($m[3])));
              break;
            }
          }
          if (empty($matches))
          {
            $content = strip_tags(htmlspecialchars_decode($content));
            $len     = $defCntr ? 1000 : $cLength;
            if ($len < mb_strlen($content))
              $content = mb_substr($content, 0, $len)."...";
            $content = CHtml::tag("p", array(), $content);
          }
          $defCntr = false;

          $attrs["comments"] = $post->getCommentsTotal();

          if ($mainImg = $post->getMainPhoto())
            $attrs += array("photo" => $mainImg);
        }

        if ($this->post)
          $attrs += array("type" => $type, "score" => Authority::model()->userScore($post->id));

        self::openNode("post", $attrs);

        self::node("title", $post->title);
        self::autorNode($post->user);
        self::node("content", $content, array("cdata" => true));

        if (!$this->announcement || count($post->tags))
        {
          self::openNode("tags");
          array_walk($post->tags, create_function('Tag $tag', 'XMLWidget::htmlNode("tag", $tag->name->name, array("id" => $tag->tag_id));'));
          self::closeNode("tags");
        }

        if (!$this->announcement || count($post->photos))
        {
          $mainImg = 0;
          $photos  = array();
          foreach ($post->photos as $photo)
          {
            $photos[$photo->order] = $photo->photo;
            if ($photo->is_main)
              $mainImg = $photo->order;
          }
          self::openNode("illustrations", array("main" => $mainImg));
          array_walk($photos, create_function('$photo', 'XMLWidget::htmlNode("illustration", $photo);'));
          self::closeNode("illustrations");
        }

        if (!$this->announcement)
          $c->widget("Comments", array("comments" => $post->commentaries));

        self::closeNode("post");
      }

      if (!$this->post)
        self::closeNode("posts");
    }
  }

?>
